self.importScripts("spark-md5.min.js");

onmessage = function (e) {
  const { chunks } = e.data;
  const fileReader = new FileReader();
  let crruentIndex = 0;
  const len = chunks.length;
  const spark = new SparkMD5.ArrayBuffer();

  function loadNext() { 
    fileReader.readAsArrayBuffer(chunks[crruentIndex].file);
  }

  fileReader.onload = (e) => {
    spark.append(e.target.result);  
     
    crruentIndex++;
    if (crruentIndex < len) {
      this.postMessage({
        progress: Number(((crruentIndex / len) * 100).toFixed(2)),
      }); 
      return loadNext();
    }

    // 最终值
    this.postMessage({
      progress: 100,
      hash: spark.end(),
    });
  };

  loadNext();
};
